#include <arpa/inet.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>

#define SIZE 100
#define MAX_CLIENTS 5

using namespace  std;
char buffer[SIZE];
pthread_rwlock_t lock;



void talk_with_client(int socket) {
	
}

int main(int argc, char** argv) {
	cout << "Echo server" << endl;
    int sckt = socket (AF_INET, SOCK_DGRAM, 0);
    
	sockaddr_in *server = new sockaddr_in;
	server->sin_family = AF_INET;
	server->sin_addr.s_addr = INADDR_ANY;
	server->sin_port = htons (atoi(argv[1]));
	
	bind(sckt,(sockaddr*)server,sizeof(sockaddr_in));
	
	
	while(true) {	
		sockaddr_in client;
		int size = 0;
		socklen_t struct_size = sizeof(client);
		size = recvfrom(sckt, buffer, SIZE, 0, (struct sockaddr *)&client, &struct_size);
		cout << "From client:" << buffer << endl;
		if(size <= 0) {
			if(size < 0)
				perror("Receive: ");			
		} else {	
			size = sendto(sckt, buffer,SIZE, 0, (struct sockaddr *)&client, struct_size);
			if(size < 0) {
				perror("Send: ");
			}
		}
	}
	return 0;
}

